function main_window(G,pid, sid,INDIR,OUTDIR)
%% Load Basic Feature Data
fprintf('%-6s %-6s %-20s Task (',pid,sid,'main_window');
indir=[G.DIR.DATA G.DIR.SEP INDIR];
infile=[pid '_' sid '_' G.FILE.BASICFEATURE_MATNAME];
outdir=[G.DIR.DATA G.DIR.SEP OUTDIR];
outfile=[G.RUN.MODEL.STUDYTYPE '_' pid '_' sid '_' G.RUN.MODEL.NAME '_' G.FILE.WINDOW_MATNAME];

load ([indir G.DIR.SEP infile]);
if G.RUN.WINDOW.LOADDATA==1 & exist([outdir G.DIR.SEP outfile],'file')==2
    load([outdir G.DIR.SEP outfile]);
else
    W=B;
end

W.NAME=['WINDOW[' G.STUDYNAME ' ' pid ' ' sid ']'];

%% Segmentation
%W=segmentbycycle(D.sensor(SENSOR.R_RIPID).sample,D.sensor(SENSOR.R_RIPID).timestamp);
if isfield(W,'sensor'), W=rmfield(W,'sensor');end;
if strcmp(G.RUN.MODEL.WINDOWTYPE,'cycle')
    W=segmentbycycle(G,MODEL);
elseif strcmp(G.RUN.MODEL.WINDOWTYPE,'time')
    W.window=segmentinwindow(G,B.sensor,B.starttimestamp,B.endtimestamp);
end;

if isempty(dir(outdir))
    mkdir(outdir);
end

save([outdir G.DIR.SEP outfile],'W');
